<?php

namespace app\admin\validate;

use app\common\business\Category;
use app\common\lib\Captcha;
use app\common\lib\Str;
use think\Validate;

class HousePerson extends Validate
{
    protected $rule = [
        'id' => 'require',
        'status' => ['require', "in" => "0,1"],
        'house_ids' => 'require|checkIds',
        'person_name' => 'require',
        'person_idcard' => 'require|checkIdCard',
        'person_phone' => 'require|checkPhone',
        'is_host' => ['require', "in" => "1,2"],
    ];

    protected $scene=[
        'create'=>['status','house_ids','person_name','person_idcard','person_phone','is_host'],
        'update'=>['id','status','house_ids','person_name','person_idcard','person_phone','is_host'],
    ];

    protected $message = [
        'id' => 'ID不能为空',
        'house_ids' => '房屋相关ID不能为空',
        'person_name' => '住户姓名不能为空',
        'person_idcard' => '住户身份证号不能为空',
        'person_phone' => '住户手机号不能为空',
        'is_host.require' => '是否业主必须',
        'is_host.in' => '是否业主数值错误',
        'status.require' => '状态必须',
        'status.in' => '状态数值错误',
    ];

    protected function checkIds($value, $rule, $data = [])
    {
        $id_arr = explode(',',$value);

        if (count($id_arr) != 4){
            return '房屋数据错误';
        }

        $houseBis = new \app\common\business\House();

        $map = [
            ['id','in',$id_arr]
        ];
        $data = $houseBis->getListByWhere($map,0,0,'id,pid,name');
        if (empty($data) || count($data) != 4){
            return '未查询到房屋数据';
        }

        return true;
    }

    protected function checkPhone($value, $rule, $data = [])
    {
        $preg = "/^(1\d{10})$/i";
        if (!preg_match($preg,$value)) {
            return "手机号格式错误";
        }
        return true;
    }

    protected function checkIdCard($value, $rule, $data = [])
    {
        if (!Str::idcardCheck($value)) {
            return "身份证号无效";
        }
        return true;
    }
}
